/*
 * 作者: 侯晓宇
 * 时间: 2017-02-16 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var EventUtils = require('app/utils/EventUtils');
	var photoBrowser = api.require('photoBrowser');
	var UIScrollPicture = api.require('UIScrollPicture');
	var ctl = {};
	var ppt_images = [];
	
	var currentIndex = 0;
	
	var splitPPTImages = function(pages){
		var images = [];
		for(var i = 0; i< pages.length; i++){
			if(pages[i].type == "img"){
				var url = c.handleUrl(pages[i].pageurl);
				if(api.systemType == 'ios'){
					url = decodeURIComponent(url);
				}
				images.push(url);
			}
		}
		return images;
	}
	var triggerGoto = function(index){
		currentIndex = index;
		//sendEvent to update pages status
		var data = {index:currentIndex,total:ppt_images.length};
		EventUtils.send(api.winName, 'updatePPTstatus', data);
	}
	
	var browsePic = function(){
		photoBrowser.open({
			activeIndex: currentIndex,
		    images: ppt_images,
		    placeholderImg: 'widget://image/picture_loading.png',
		    bgColor: '#000'
		}, function(ret, err) {
		
			if(ret.eventType == "click"){
				photoBrowser.close();
				
				api.setScreenOrientation({
    				orientation: 'portrait_up'
				});
				
				setTimeout(function(){
					api.setScreenOrientation({
		    			orientation: 'auto'
					});
					
				},1500);
				
			}
		   
		});	
	}
	
	
	
	
	//监听横屏事件
	window.addEventListener("orientationchange", function() {
		console.log("Init,W:"+api.winWidth+",H:"+api.winHeight+",orientation:"+window.orientation+',System:'+api.systemType);
		
		if(window.orientation == 90 || window.orientation == -90){
			//横屏
			browsePic();
		}else if(window.orientation == 0){
			//竖屏
			photoBrowser.close();
		}

    }, false);
	
	
	
	ctl.goTo = function(index){
		UIScrollPicture.setCurrentIndex({
		    index: parseInt(index)
		});
		triggerGoto(index);
	}
	
	ctl.init = function(pages){
		ppt_images = splitPPTImages(pages);

		api.openFrame({
	        name: 'RoomPPTCtlFrm',
	        url: './RoomPPTCtlFrm.html',
	        rect: {
		        x:0,
		        y:api.frameHeight-c.remToPx(2),
		        w:c.remToPx(4),
		        h:c.remToPx(2)
	        },
	        bgColor: 'rgba(0,0,0,0)'
        });
        api.bringFrameToFront({from: 'RoomStagingFrm'});
        
		
		
		UIScrollPicture.open({
			rect: {
				x: 0,
				y: 0,
				w: api.winWidth,
				h: api.frameHeight
			},
			data: {
				paths: ppt_images
			},
			placeholderImg: 'widget://res/imgplaceholder.png',
			contentMode: 'scaleAspectFit',
			auto: false,
			fixedOn: api.frameName,
			loop: false,
			fixed: false
		}, function(ret, err) {
			if (ret) {
				if(ret.eventType == "click"){
					browsePic();
				}
				
			}
		});

		UIScrollPicture.addEventListener({
			name: 'scroll'
		}, function(ret, err) {
			if (ret) {
				triggerGoto(ret.index);
			} 
		});
		setTimeout(function(){
			triggerGoto(0);
		},1000);
		
		
		//设置横屏与竖屏切换
		api.setScreenOrientation({
    		orientation: 'auto'
		});

	}

	return ctl;
});